package net.ikenway.springboot.demo.Controller;

import net.ikenway.springboot.demo.Model.Dto.AddressDto;
import net.ikenway.springboot.demo.Model.Dto.ResponseBodyDto;
import net.ikenway.springboot.demo.Service.Implement.AddressServiceImpl;
import net.ikenway.springboot.demo.Utils.ValidateErrorMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityNotFoundException;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author MasterKenway <zk@ikenway.net>
 * @Description
 * @date Created Date 12/19/2020
 */
@RestController
@RequestMapping(value = "/api/addr")
public class AddressController {
    private final AddressServiceImpl addressService;

    @Autowired
    AddressController(AddressServiceImpl addressService) {
        this.addressService = addressService;
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public ResponseBodyDto findAll() {
        try {
            return new ResponseBodyDto(200, "查询成功", addressService.findAll());
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseBodyDto create(@Valid @RequestBody AddressDto addressDto, BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors()) throw new Exception(ValidateErrorMsgUtil.getFirstFormatMsg(bindingResult.getAllErrors()));

            addressService.create(addressDto);
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }
        return new ResponseBodyDto(200, "新增地址成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseBodyDto update(@RequestBody AddressDto addressDto) {
        try {
            if (addressDto.getAddressId() == null) throw new Exception("地址ID不能为空");

            addressService.update(addressDto);
        } catch (EntityNotFoundException entityNotFoundException) {
            return new ResponseBodyDto(500, "地址不存在");
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }

        return new ResponseBodyDto(200, "修改成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseBodyDto delete(@RequestBody Map<String, List<Long>> requestData) {
        try {
            List<Long> addressIds = requestData.get("addressIds");
            if (addressIds == null || addressIds.isEmpty()) throw new Exception("数组不能为空");

            addressService.delete(requestData.get("addressIds"));
        } catch (EntityNotFoundException entityNotFoundException) {
            return new ResponseBodyDto(500, "地址不存在");
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }
        return new ResponseBodyDto(200, "删除成功");
    }
 }
